package picture

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	"chain_bridge_service/internal/services/picture"
	"net/http"
)

type createResponse struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

type createRequest struct {
	Id         string `json:"id"`
	FileName   string `json:"fileName"`
	SkipUrl    string `json:"skipUrl"`
	PictureUrl string `json:"pictureUrl"`
}

type listRequest struct {
	PageIndex uint `form:"pageIndex"` // 第几页
	PageSize  uint `form:"pageSize"`  // 每页显示条数
}

type Page struct {
	Total int64       `json:"total"`
	List  interface{} `json:"list"`
}

type deleteRequest struct {
	Id       string
	FileName string
}

func (h *handler) Create() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRequest)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		data := new(picture.PicturesData)
		data.PictureUrl = req.PictureUrl
		data.FileName = req.FileName
		data.SkipUrl = req.SkipUrl

		err := h.pictureService.Create(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.CreateUserError,
				code.Text(code.CreateUserError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) Update() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRequest)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		data := new(picture.PicturesData)
		data.Id = req.Id
		data.FileName = req.FileName
		data.SkipUrl = req.SkipUrl
		data.PictureUrl = req.PictureUrl

		err := h.pictureService.Update(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.CreateUserError,
				code.Text(code.CreateUserError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) List() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listRequest)
		res := new(createResponse)

		if err := ctx.ShouldBindQuery(&req); err != nil {

			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		productList, err := h.pictureService.List(ctx, req.PageIndex, req.PageSize)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetApplyTokenError,
				code.Text(code.GetApplyTokenError)).WithError(err),
			)
			return
		}

		count, err := h.pictureService.Count(ctx)

		page := new(Page)
		page.Total = count
		page.List = productList

		res.Code = code.StatusOK
		res.Data = page
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) Delete() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(deleteRequest)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		createData := new(picture.PicturesData)

		createData.Id = req.Id
		createData.FileName = req.FileName
		err := h.pictureService.Delete(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.DeleteProductError,
				code.Text(code.DeleteProductError)).WithError(err),
			)
			return
		}
		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) Upload() core.HandlerFunc {
	return func(ctx core.Context) {
		res := new(createResponse)
		file, err := ctx.GetFormFile("file")
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.CreateUserError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		url, name, err := picture.Upload(ctx, file)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.DeleteProductError,
				code.Text(code.DeleteProductError)).WithError(err),
			)
			return
		}

		data := make(map[string]string)

		data["url"] = url
		data["fileName"] = name
		res.Code = code.StatusOK
		res.Data = data
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)

	}

}
